package com.aegean.CommercialHotelBookSystem.shop.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aegean.CommercialHotelBookSystem.factory.BusinessFactory;
import com.aegean.CommercialHotelBookSystem.factory.DaoFactory;
import com.aegean.CommercialHotelBookSystem.factory.MyTools;
import com.aegean.CommercialHotelBookSystem.model.Room;
import com.aegean.CommercialHotelBookSystem.shop.business.IShopBusiness;

/**
 * Servlet implementation class ShopHotel_SearchHotel
 */
public class ShopHotel_SearchHotel extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShopHotel_SearchHotel() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String hotel_Name = MyTools
				.toGB2312(request.getParameter("hotel_Name"));
		IShopBusiness business = BusinessFactory.createShopBusiness();
		List<Room> rooms = business.searchHotelRooms2(hotel_Name);
		int pagesize = 8;
		int pagecount = (rooms.size() + pagesize - 1) / pagesize;
		List<Room> requestRooms = new ArrayList<Room>();
		for (int i = 0; i < pagesize; i++) {
			if (i == rooms.size()) {
				break;
			} else {
				requestRooms.add(rooms.get(i));
			}
		}
		if (rooms.isEmpty()) {
			request.getRequestDispatcher("Hotel_index.jsp").forward(
					request, response);
		} else {
			session.setAttribute("Hotel_ClickHotel_Id", rooms.get(0)
					.getRoom_Hotel().getHotel_Id());
			request.setAttribute("Hotel_ClickRooms", requestRooms);
			session.setAttribute("Hotel_ClickRooms_page", pagecount);
			session.setAttribute("Hotel_ClickRooms", rooms);
			request.getRequestDispatcher("Hotel_ClickhotelRooms.jsp").forward(
					request, response);
		}
	}

}
